using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

/// <summary>
/// 引导摄像机节点
/// </summary>
public class CameraPathTutorialNode : TutorialEventNode
{
    [JsonAttribute("path", "摄像机prefab路径")]
    public string cameraPrefabPath = "";

    [JsonAttribute("lctype", "摄像机定位方式")]
    public ElementType cameraLocateType = ElementType.areaIndex;

    [JsonAttribute("arIn", "地块下标","lctype", "", "areaIndex")]
    public Vector2Int areaIndex = new Vector2Int(-1,-1);

    [JsonAttribute("bdID", "建筑ID", "lctype", "", "buildingID")]
    public int buildingId = 0;

    public string realPath = "";

    public CameraPathBezierAnimator cameraAnimator;
    public GameObject cacheObj;

    public static PinchTwistMove cameraController {
        get {
            if(_cameraController == null)
            {
                _cameraController = Camera.main.transform.parent.Find("Pinch & Twist & Move").GetComponent<PinchTwistMove>();
            }
            return _cameraController;
        }
    }
    public static PinchTwistMove _cameraController;


    protected override void Cancel()
    {
        this.cameraAnimator.Seek(1);
    }

    protected override void Init()
    {
        this.realPath = UITools.GetResourcePath(this.cameraPrefabPath);
        this.cameraAnimator = GameObject.Instantiate(Resources.Load<GameObject>(this.realPath)).GetComponent<CameraPathBezierAnimator>();
        this.InitStart();
        this.cameraAnimator.enabled = true;
        this.cameraAnimator.Play();
        this.cameraAnimator.AnimationFinished += this.AnimationOnEnd;
    }

    protected void AnimationOnEnd()
    {
        if (cacheObj)
        {
            GameObject.Destroy(cacheObj);
            cacheObj = null;
        }
        cameraController.ResumeUpdate();
        this.cameraAnimator.AnimationFinished -= this.AnimationOnEnd;
        Shoot();
    }

    protected void InitStart()
    {

        this.cameraAnimator.animationTarget = Camera.main.transform;
        Hope.EventDispatcher.DispatchEvent(Hope.EventRegisterName.CameraNoCanOperation);
        cameraController.StopUpdate();
        switch (cameraLocateType)
        {
            case ElementType.areaIndex:
                this.cameraAnimator.transform.position = CustomBuildUtils.GetWorldPosByValume(UserManager.Instance.AreaId, areaIndex.x, areaIndex.y, 0.5f);

                break;
            case ElementType.buildingID:
                var data = Building.BuildingManager.Instance.GetSelfBuildingbyConfigId(buildingId);
                if (data == null)
                {
                    Debug.LogError("Building id not find is Game, Id is :" + buildingId);
                    return;
                }
                var buildingObj = CustomBuildManager.Instance.GetGobehaivour(data.buildingID);
                this.cameraAnimator.transform.position = buildingObj.position;
                break;
            default:
                Debug.LogError("locate error");
                break;
        }
    }
}


